﻿<?php

class sfValidatedPicture extends sfValidatedFile{

  public function save($file = null, $fileMode = 0666, $create = true, $dirMode = 0777) {

	  $filename = parent::save($file, $fileMode, $create, $dirMode);

	  if ( $filename )
	  {
		  $filepath = sfConfig::get('sf_upload_dir').'/'.$filename;
		  $thumbnailpath = sfConfig::get('sf_thumbnail_dir').'/'.$filename;

/*
		  if ($this->getObject()->getFile() && $this->getObject()->getFile() != ""){
			  $oldfilepath = sfConfig::get('sf_upload_dir').'/'.$$this->getObject()->getFile();

			  if (file_exists($oldfilepath) )
			  {
				  unlink($oldfilepath);
			  }

			  $oldthumbnailpath = sfConfig::get('sf_thumbnail_dir').'/'.$this->getObject()->getFile();
			  if ( file_exists($oldthumbnailpath) )
			  {
				  unlink($oldthumbnailpath);
			  }
		  }
		  */

		  //$upload->save($filepath);

		  $original = new sfThumbnail(400, 250, true, true, 75, 'sfGDAdapter');
		  $original->loadFile($filepath);
		  $original->save($filepath);

		  $thumbnail = new sfThumbnail(90, 90, true, true, 75, 'sfGDAdapter');
		  $thumbnail->loadFile($filepath);
		  $thumbnail->save($thumbnailpath);
	  }
	  /*
	  $delete = $this->getValue('file_delete');
	  if ( $delete )
	  {
		  $filename = $this->getObject()->getFile();
		  $filepath = sfConfig::get('sf_upload_dir').'/'.$filename;
		  @unlink($filepath);
		  $this->getObject()->delete();

		  $thumbnailpath = sfConfig::get('sf_thumbnail_dir').'/'.$filename;
		  @unlink($thumbnailpath);

	  }*/


      return $filename;
  }
}
